package com.yj.cloud.common.core.handler;

import com.yj.cloud.common.core.domain.R;
import com.yj.cloud.common.core.exception.BaseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.ArrayList;
import java.util.List;

@RestController
@RestControllerAdvice
public class DefaultExceptionHandlerConfig {
    private static final Logger logger = LoggerFactory.getLogger(DefaultExceptionHandlerConfig.class);

    @ExceptionHandler({ MethodArgumentNotValidException.class, BindException.class })
    public ResponseEntity<R<List<String>>> methodArgumentNotValidExceptionHandler(Exception e) {
        logger.error("methodArgumentNotValidExceptionHandler", e);
        List<FieldError> fieldErrors = null;
        if (e instanceof MethodArgumentNotValidException) {
            fieldErrors = ((MethodArgumentNotValidException) e).getBindingResult().getFieldErrors();
        }
        if (e instanceof BindException) {
            fieldErrors = ((BindException) e).getBindingResult().getFieldErrors();
        }
        if (fieldErrors == null) {
            return ResponseEntity.status(HttpStatus.OK)
                    .body(R.fail());
        }

        List<String> defaultMessages = new ArrayList<>(fieldErrors.size());
        for (FieldError fieldError : fieldErrors) {
            defaultMessages.add(fieldError.getField() + ":" + fieldError.getDefaultMessage());
        }
        return ResponseEntity.status(HttpStatus.OK)
                .body(R.fail(defaultMessages));
    }

    @ExceptionHandler({ HttpMessageNotReadableException.class })
    public ResponseEntity<R<List<FieldError>>> methodArgumentNotValidExceptionHandler(
            HttpMessageNotReadableException e) {
        logger.error("methodArgumentNotValidExceptionHandler", e);
        return ResponseEntity.status(HttpStatus.OK)
                .body(R.fail(""));
    }

    @ExceptionHandler(BaseException.class)
    public ResponseEntity<R<Object>> mall4cloudExceptionHandler(BaseException e) {
        logger.error("mall4cloudExceptionHandler", e);


        // 失败返回消息 状态码固定为直接显示消息的状态码
        return ResponseEntity.status(HttpStatus.OK).body(R.fail(e.getMessage()));
    }

//    @ExceptionHandler(Exception.class)
//    public ResponseEntity<R<Object>> exceptionHandler(Exception e) throws TransactionException {
//        logger.error("exceptionHandler", e);
//        logger.info("RootContext.getXID(): " + RootContext.getXID());
//        if (StrUtil.isNotBlank(RootContext.getXID())) {
//            GlobalTransactionContext.reload(RootContext.getXID()).rollback();
//        }
//        return ResponseEntity.status(HttpStatus.OK).body(R.fail(ResponseEnum.EXCEPTION));
//    }
}
